package com.rc.saas.tenant.controller.tenant;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.controller.banner.TenantBannerController;
import com.rc.saas.tenant.model.app.TenantAppUpgrade;
import com.rc.saas.tenant.model.tenant.TenantCompanyInfo;
import com.rc.saas.tenant.model.tenant.TenantCompanyInfoExample;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.service.app.TenantAppInfoService;
import com.rc.saas.tenant.service.tenant.TenantCompanyInfoService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author frog
 * @date 2021/4/15
 */
@Controller
@RequestMapping("/tenantCompanyInfo")
public class TenantCompanyInfoController {
    private static Logger logger = LogManager.getLogger(TenantBannerController.class);


    @Autowired
    private TenantCompanyInfoService tenantCompanyInfoService;


    /**
     * 跳转子公司列表页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        TenantUser tenantUser = ShiroUtils.getSessionUser();
        TenantCompanyInfoExample example = new TenantCompanyInfoExample();
        //公司类型 0主公司 1子公司 默认0
        example.createCriteria().andTenantCodeEqualTo(tenantUser.getTenantCode()).andCompanyTypeEqualTo("0");
        List<TenantCompanyInfo> tenantCompanyInfos = tenantCompanyInfoService.selectByExample(example);
        if (!CollectionUtils.isEmpty(tenantCompanyInfos)) {
            //查出子公司ID
            model.addAttribute("tenantCompanyInfo", tenantCompanyInfos.get(0));
        }
        return "/templates/tenant/company/tenantCompanyInfo";
    }


    /**
     * 返回子公司列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageTenantCompanyInfo", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageTenantCompanyInfo(BaseSearch baseSearch) {
        PageInfo<TenantCompanyInfo> pageInfo = tenantCompanyInfoService.listPageTenantCompanyInfo(baseSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 跳转增加子公司页面
     *
     * @return
     */
    @RequestMapping(value = "/addTenantCompanyInfo", method = RequestMethod.GET)
    public String addTenantCompanyInfo(Model model) {
        TenantUser tenantUser = ShiroUtils.getSessionUser();
        TenantCompanyInfoExample example = new TenantCompanyInfoExample();
        //公司类型 0主公司 1子公司 默认0
        example.createCriteria().andTenantCodeEqualTo(tenantUser.getTenantCode()).andCompanyTypeEqualTo("0");
        List<TenantCompanyInfo> tenantCompanyInfos = tenantCompanyInfoService.selectByExample(example);
        if (!CollectionUtils.isEmpty(tenantCompanyInfos)) {
            //查出子公司ID
            model.addAttribute("tenantCompanyInfo", tenantCompanyInfos.get(0));
        }
        return "/templates/tenant/company/addTenantCompanyInfo";
    }

    /**
     * 跳转编辑子公司页面
     *
     * @return
     */
    @RequestMapping(value = "/editTenantCompanyInfo/{tenantCompanyId}", method = RequestMethod.GET)
    public String editTenantCompanyInfo(@PathVariable("tenantCompanyId") Long tenantCompanyId, Model model) {
        TenantCompanyInfo tenantCompanyInfo = tenantCompanyInfoService.selectByPrimaryKey(tenantCompanyId);
        model.addAttribute("tenantCompanyInfo", tenantCompanyInfo);
        return "/templates/tenant/company/editTenantCompanyInfo";
    }

    /**
     * 跳转子公司详情页面
     *
     * @return
     */
    @RequestMapping(value = "/detailTenantCompanyInfo/{tenantCompanyId}", method = RequestMethod.GET)
    public String detailTenantCompanyInfo(@PathVariable("tenantCompanyId") Long tenantCompanyId, Model model) {
        TenantCompanyInfo tenantCompanyInfo = tenantCompanyInfoService.selectByPrimaryKey(tenantCompanyId);
        model.addAttribute("tenantCompanyInfo", tenantCompanyInfo);
        return "/templates/tenant/company/detailTenantCompanyInfo";
    }


    /**
     * 保存/编辑子公司信息
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateTenantCompanyInfo", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateTenantCompanyInfo(TenantCompanyInfo tenantCompanyInfo) {
        try {
            if (tenantCompanyInfo.getTenantCompanyId() != null) {
                tenantCompanyInfoService.updateByPrimaryKeySelective(tenantCompanyInfo);
            } else {
                //公司类型 0主公司 1子公司 默认0
                tenantCompanyInfo.setCompanyType("1");
                tenantCompanyInfoService.insertSelective(tenantCompanyInfo);
            }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存子公司信息失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

    /**
     * 删除子公司信息
     */
    @ResponseBody
    @RequestMapping(value = "/deleteTenantCompanyInfo", method = RequestMethod.POST)
    public TResponse deleteTenantCompanyInfo(Long tenantCompanyId) {
        try {
            tenantCompanyInfoService.deleteByPrimaryKey(tenantCompanyId);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("删除子公司信息失败", e);
            return TResponse.FAIL("删除子公司信息失败");
        }
    }
}
